function ans = lab5ej3()
	
    x =  0 : 0.5 : 2*pi;
	y = [];

	for i = x
		y =[y,senint(i)];
	endfor	

	plot(x,sin(x),'r',x,y,'b');
        
endfunction

function y = senint(x);

	if x != 0
		[N, N1,N2] = getN(x);
		if N != 0
			y = trapecio_compuesta('cos',0, x, N);
		else		
			y = trapecio_compuesta('cos',0, x, N1) + trapecio_compuesta('cos',0, x, N2);
		endif
	else 
		y = 0;
	endif

endfunction

function [N, N1, N2] = getN(x)

	N = 0;
	N1 = 0;
	N2 = 0;
	
	if(floor(x) == 2 && ceil(x) == 2)
		i = 1/2;
	elseif(floor(x) == 5 && ceil(x) == 5)
		i = 3/2;
	else 
		N = x/0.1;
	endif		

	if(N == 0)
		N1 = round((x - (i*pi))/0.1);
		N2 = round(((i*pi)-x - 0.5)/0.1);
	endif	
		
endfunction		

function res = trapecio_compuesta(fun,a, b, N)

	h = (b-a)/N;
    sum = 0;

    for i = 1:N-1
        x = a + i*h;
        sum += feval(fun,x);
    endfor

	res = (h/2) * (feval(fun,a) + 2*sum + feval(fun,b));
    
endfunction
